package com.zfast.forge.qlExpress;

import com.zfast.forge.annotation.Operator;
import com.zfast.forge.pojo.CustomBigDecimal;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.stream.Stream;

/**
 * 定义常用公式
 *
 * @author zgn
 * @Description: BadFunctions
 * @date 2024/2/29 0029
 */
@Slf4j
public class BadFunctions {
    /**
     * 获取多元平方和的开更:√([1,2,2])
     *
     * @author zgn
     * @date 2024/2/29 0029
     * @param    obj
     * @return double
     */
    @Operator("√")
    public static BigDecimal squareRoot(Object... obj) {
        Double reduce = Stream.of(obj)
                //批量转Double
                .map(item -> Double.valueOf(item.toString()))
                //(x^2+...)
                .reduce(0d, (subtotal, element) -> {
                            //subtotal 上一个参数的结果,element新元素的操作
                            return subtotal + Math.pow(element, 2);
                        }
                );

        BigDecimal sqrt = CustomBigDecimal.valueOf(new BigDecimal(Math.sqrt(reduce)));
        //log.debug("squareRoot:"+sqrt);
        return sqrt;
    }

    @Operator("square")
    public static BigDecimal square(Object obj) {
        BigDecimal square = CustomBigDecimal.valueOf(new BigDecimal(obj.toString()).pow(2));
        //log.debug("square:"+square);
        return square;
    }
}
